package com.tinyideatech.admin.controller.car;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.CarQrcode;
import com.tinyideatech.system.service.ICarQrcodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author zhangy-5
 * @date 2019-09-09
 */

@Controller
@RequestMapping("/system/qrcode")
public class CarQrcodeController extends BaseController {

    @Autowired
    private ICarQrcodeService carQrcodeService;

    @GetMapping()
    public String list() {
        return "system/qrcode/list";
    }

    @GetMapping("/add")
    public String add() {
        return "system/qrcode/add";
    }

    @GetMapping("/edit/{id}")
    public String carQrcodeEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        CarQrcode carQrcode = carQrcodeService.getById(id);
        map.addAttribute("carQrcode", carQrcode);
        return "system/qrcode/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(CarQrcode carQrcode) {
        QueryWrapper<CarQrcode> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty("")) {
            qw.like("", "");
        }
        IPage<CarQrcode> page = startPage();
        IPage<CarQrcode> carQrcodeList = carQrcodeService.page(page, qw);
        return page(carQrcodeList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            CarQrcode sysRole
    ) {
        carQrcodeService.saveOrUpdate(sysRole);
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ) {
        Long[] carQrcodeIds = Convert.toLongArray(ids);
        carQrcodeService.removeByIds(Arrays.asList(carQrcodeIds));
        return success();
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public ApiResult changeStatus(CarQrcode carQrcode) {
        carQrcodeService.changeStatus(carQrcode);
        return success();
    }

}
